package com.zrq.jwtdemo.core.aop;

import com.zrq.jwtdemo.core.exception.AppException;
import com.zrq.jwtdemo.core.prop.JwtProp;
import com.zrq.jwtdemo.core.utils.JwtUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.SourceLocation;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * @Author:
 * @CreateDate: 2020/12/14
 * @Version: 类说明：aop拦截器
 */
@Aspect
@Component
@AllArgsConstructor
@Slf4j
public class ApplicationAop {

    private final JwtProp jwtProp;

    private final JwtUtils jwtUtils;

    @Pointcut("execution(public * com.zrq.jwtdemo.*.controller.*.*(..))")
    public void ApplicationAop() {
    }

    @Before("ApplicationAop()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String url = request.getServletPath();
        if (!"/test/login".equals(url)) {
            String token = request.getHeader(jwtProp.getHeader());
            if ("".equals(token) || token == null)
                throw new AppException("您未登录！");
            jwtUtils.verify(token);
        }
    }





}
